package com.easyweb.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.easyweb.common.IPUtils;

public class RequestInterceptor implements HandlerInterceptor{
	
	private static Logger logger = LoggerFactory.getLogger(RequestInterceptor.class);

	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		
	}

	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
			throws Exception {
		
	}

	public boolean preHandle(HttpServletRequest request, HttpServletResponse arg1, Object arg2) throws Exception {
		if(!StringUtils.isEmpty(request.getRequestURI())&&!request.getRequestURI().contains("/index/")&&!request.getRequestURI().equals("/")){
			//访问后台管理
			logger.info(request.getRequestURI());
			try {
				logger.info("IP:"+IPUtils.getRemoteIp(request));
			} catch (Exception e) {
				logger.error("获取ip失败!");
			}
		}
		return true;
	}

}
